<?php

/**
 * Doctrine acl adapter
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Doctrine.php 38 2009-02-18 17:12:39Z monzee $
 */
class Admin_Acl_Doctrine
{
    /**
     *
     * @param string $username
     * @return Admin_Doctrine_User User details
     */
    public function getUserByUserName($username)
    {
        $q = Doctrine_Query::create()
            ->from('Admin_Doctrine_User u')
            ->leftJoin('u.Roles')
            ->where('u.username = ?', $username)
        ;

        return $q->fetchOne();
    }

    /**
     *
     * @param Admin_Doctrine_User $user
     * @return Doctrine_Collection Array of role details
     */
    public function getUserRoles($user)
    {
        $roles = $user->Roles;
        return $roles;
    }

    /**
     *
     * @param mixed $roleId
     * @return Admin_Doctrine_Role Role details
     */
    public function getRoleById($roleId)
    {
        $q = Doctrine_Query::create()
            ->from('Admin_Doctrine_Role r')
            ->leftJoin('r.Parents')
            ->where('r.id = ?', $roleId);
        return $q->fetchOne();
    }

    /**
     *
     * @param Admin_Doctrine_Role $role
     * @return Doctrine_Collection Array of role details
     */
    public function getParentRoles($role)
    {
//        var_dump($role);die;
        $parents = $role->Parents;
        return $parents;
    }

    public function getAllRoles()
    {
        $q = Doctrine_Query::create()
            ->from('Admin_Doctrine_Role r')
            ->setHydrationMode(Doctrine::HYDRATE_ARRAY);

        return $q->execute();
    }

    /**
     *
     * @param array $res Array containing res id and priv
     * @return Doctrine_Collection Array of Permission rows
     */
    public function getRulesByResource($res)
    {
        $resId = $res['id'];
        $priv = $res['privilege'];

        $q = Doctrine_Query::create()
            ->from('Admin_Doctrine_Permission p')
            ->where('p.resource_id = ?', $resId);

        if (empty($priv)) {
            $q->andWhere('p.privilege IS NULL');
        } else {
            $q->andWhere('p.privilege = ?', $priv);
        }

        return $q->execute();
    }

    /**
     *
     * @return Doctrine_Collection Array of permission details
     */
    public function getAllPermissions()
    {
        $q = Doctrine_Query::create()
            ->from('Admin_Doctrine_Permission p');
        return $q->execute();
    }

    protected $_resourceNameSpec = ':module/:controller';
    protected $_resourcePrivSpec = ':action';

    /**
     * Generate resource id and priv
     *
     * $params must contain the keys 'module', 'controller' and 'action'
     *
     * @param array $params Uses the current request if not passed
     * @return array containing resourceId and privilege
     */
    public function generateResource(array $params = null)
    {
        if (null !== $params) {
            $module = $params['module'];
            $controller = $params['controller'];
            $action = $params['action'];
        } else {
            $req = Zend_Controller_Front::getInstance()->getRequest();
            $module = $req->getModuleName();
            $controller = $req->getControllerName();
            $action = $req->getActionName();
        }

        $replaceMap = array(
            ':module' => $module,
            ':controller' => $controller,
            ':action' => $action,
        );

        $resId = str_replace(
            array_keys($replaceMap),
            array_values($replaceMap),
            $this->_resourceNameSpec
        );

        $priv = str_replace(
            array_keys($replaceMap),
            array_values($replaceMap),
            $this->_resourcePrivSpec
        );

        return array('id' => $resId, 'privilege' => $priv);
    }

    public function getResource($params = null)
    {
        return $this->generateResource($params);
    }

}
